package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.RedisCache;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author xiongkunYang
 */
@Tag(name = "分类管理")
@RestController
@RequestMapping(value = "/api/album")
public class BaseCategoryApiController {

    private final BaseCategoryService baseCategoryService;

    public BaseCategoryApiController(BaseCategoryService baseCategoryService) {
        this.baseCategoryService = baseCategoryService;
    }

    @Operation(summary = "查询所有分类（1、2、3级分类）")
    @GetMapping("/category/getBaseCategoryList")
    public Result<List<JSONObject>> getBaseCategoryList() {
        return Result.ok(this.baseCategoryService.getBaseCategoryList());
    }

    @Operation(summary = "根据一级分类Id获取分类属性（标签）列表")
    @Parameters({
            @Parameter(name = "category1Id", description = "一级分类Id", required = true)
    })
    @GetMapping("category/findAttribute/{category1Id}")
    public Result<List<BaseAttribute>> findAttributesByCategory1Id(@PathVariable Long category1Id) {
        List<BaseAttribute> baseAttributes = baseCategoryService.getAttributesByCategory1Id(category1Id);
        return Result.ok(baseAttributes);
    }

    @Operation(summary = "根据三级分类Id 获取到分类信息")
    @Parameters({
            @Parameter(name = "category3Id", description = "三级分类Id", required = true)
    })
    @GetMapping("/category/getCategoryView/{category3Id}")
    public Result<BaseCategoryView> getCategoryView(@PathVariable Long category3Id) {
        BaseCategoryView baseCategoryView = baseCategoryService.getCategoryViewByCategory3Id(category3Id);
        return Result.ok(baseCategoryView);
    }

    @Operation(summary = "查询指定1级分类下置顶前7个三级分类集合")
    @Parameters({
            @Parameter(name = "category1Id", description = "1级分类id", required = true)
    })
    @GetMapping("/category/findTopBaseCategory3/{category1Id}")
    public Result<List<BaseCategory3>> findTopBaseCategory3(@PathVariable Long category1Id) {
        List<BaseCategory3> baseCategory3List = baseCategoryService.getTopBaseCategory3(category1Id);
        return Result.ok(baseCategory3List);
    }

    @Operation(summary = "根据一级分类id获取全部分类信息")
    @Parameters({
            @Parameter(name = "category1Id", description = "1级分类id", required = true)
    })
    @GetMapping("/category/getBaseCategoryList/{category1Id}")
    public Result<JSONObject> getBaseCategoryListByCategory1Id(@PathVariable Long category1Id) {
        JSONObject baseCategoryList = baseCategoryService.getBaseCategoryListByCategory1Id(category1Id);
        return Result.ok(baseCategoryList);
    }


    @Operation(summary = "查询所有一级分类列表")
    @GetMapping("/category/findAllCategory1")
    @RedisCache(prefix = "allCategory1:")
    public Result<List<BaseCategory1>> getAllCategory1() {
        return Result.ok(baseCategoryService.list());
    }

}

